package com.example.minefieldkeepercloud.activity.treatedmachine;

import android.opengl.Visibility;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.minefieldkeepercloud.R;

import com.example.minefieldkeepercloud.activity.dailypower.DailyPowerAc;
import com.example.minefieldkeepercloud.adapter.TreateMachineAdapter;

import com.example.minefieldkeepercloud.bean.TreateMachineBean;
import com.example.minefieldkeepercloud.interfac.ApiHttpCallback;

import com.example.minefieldkeepercloud.network.HttpUtils;
import com.example.minefieldkeepercloud.network.MyGson;
import com.example.minefieldkeepercloud.network.NetHelperNew;
import com.example.minefieldkeepercloud.util.NetWorkUtil;
import com.example.minefieldkeepercloud.util.ToastUtil;
import com.example.minefieldkeepercloud.view.pop.TypeSelectPopupWindow;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 待处理矿机
 */
public class TreateMachineAc extends AppCompatActivity {
    @BindView(R.id.recyleview)
    LRecyclerView mRecyclerView;
    @BindView(R.id.tv_empty)
    TextView tv_empty;
    private TreateMachineAdapter treateMachineAdapter;
    private List<TreateMachineBean.DataBean> listData;
    private TypeSelectPopupWindow typeSelectPopupWindow;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    @BindView(R.id.tv_date)
    TextView tv_date;
    private int type=0;
    @BindView(R.id.ll_date)
    LinearLayout ll_date;
    private int num=1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_treatemachine);
        ButterKnife.bind(this);
        initview();
       // getNetData();
    }
    /**
     * 初始化
     */
    private void initview() {
        listData=new ArrayList<>();
        treateMachineAdapter=new TreateMachineAdapter(this,listData);
        lRecyclerViewAdapter = new LRecyclerViewAdapter(treateMachineAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(lRecyclerViewAdapter);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader); //设置下拉刷新Progress的样式
        // mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);  //设置下拉刷新箭头
        //设置头部加载颜色
        mRecyclerView.setHeaderViewColor(R.color.system_color, R.color.system_color, R.color.bg);
//设置底部加载颜色
        mRecyclerView.setFooterViewColor(R.color.system_color, R.color.system_color, android.R.color.white);


        mRecyclerView.setOnRefreshListener(new OnRefreshListener() {//下拉刷新
            @Override
            public void onRefresh() {
                num=1;
                listData.clear();
                lRecyclerViewAdapter.notifyDataSetChanged();
                getNetData();
            }
        });

        mRecyclerView.setLoadMoreEnabled(true);

        mRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {//加载更多
            @Override
            public void onLoadMore() {
                num++;
                getNetData();
            }
        });

        typeSelectPopupWindow = new TypeSelectPopupWindow(2,this, new TypeSelectPopupWindow.OnSelectLayoutListener() {
            @Override
            public void selectTypeLayout(int i) {

                if (i == 0) {
                    tv_date.setText("待处理");
                    type=0;
                    treateMachineAdapter.setType(0);

                } else if (i == 1) {
                    tv_date.setText("已处理");
                    type=1;
                    treateMachineAdapter.setType(1);

                }
                mRecyclerView.forceToRefresh();


            }
        });

        tv_date.setText("待处理");

        mRecyclerView.forceToRefresh();
    }

    private void getNetData()
    {

        if (!NetWorkUtil.check(this)) {

            return;
        }
        NetHelperNew.GetOre_Machine(num+"",type+"",new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                TreateMachineBean treateMachineBean = MyGson.getInstance().fromJson(data, TreateMachineBean.class);
                if (treateMachineBean.getType() == 1) {
                    if (treateMachineBean.getData().size() > 0) {
                        tv_empty.setVisibility(View.GONE);
                        listData.addAll(treateMachineBean.getData());
                        treateMachineAdapter.setListData(listData);
                        lRecyclerViewAdapter.notifyDataSetChanged();

                    } else {
                        if(num==1)
                        {
                            tv_empty.setVisibility(View.VISIBLE);
                        }
                        else
                        {
                            mRecyclerView.setNoMore(true);
                        }


                    }

                } else {
                    ToastUtil.showToast(TreateMachineAc.this, treateMachineBean.getContent());
                }
                mRecyclerView.refreshComplete(10);
            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                mRecyclerView.refreshComplete(10);
                ToastUtil.showToast(TreateMachineAc.this, msg);
            }
        });



    }

    @OnClick({R.id.title_left, R.id.ll_date})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left:
                finish();
                break;
            case R.id.ll_date:
                if (typeSelectPopupWindow != null)
                    typeSelectPopupWindow.showAsDropDown(ll_date, 0, 0);
                break;

        }
    }
}
